const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'

//获取自己部门的所有员工信息
router.post('/getSalaryChangeInfo',async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        if(response_temp.length > 0) {
            sql = 'SELECT workNumber,changeType,reason,amount,occurTime FROM salary_change_info WHERE departmentId = ?'
            let data = [response_temp[0].departmentId]
            if(req.body.workNumber!=null){
                sql = 'SELECT workNumber,changeType,reason,amount,occurTime FROM salary_change_info WHERE departmentId = ? and workNumber = ?'
                data = [response_temp[0].departmentId,req.body.workNumber]
                console.log("指定工号的查询")
                console.log(data)
            }
            //查找自己部门的员工信息
            db.base(sql, data, response => {
                res.json(response)
            })
        }else{
            res.json({
                result:"error",
                reason:"无权限"
            })
        }
    })

})

module.exports = router
